package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.printer.PrintDemo;
import com.ruoyi.system.domain.MrDeviceAlarmData;
import com.ruoyi.system.domain.vo.CurrentAlertsVO;
import com.ruoyi.system.domain.vo.MrDeviceAlarmDataVO;
import com.ruoyi.system.service.IMrDeviceAlarmDataService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备报警数据Controller
 *
 * @author lsw
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/system/data")
public class MrDeviceAlarmDataController extends BaseController {
    @Autowired
    private IMrDeviceAlarmDataService mrDeviceAlarmDataService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询设备报警数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(MrDeviceAlarmData mrDeviceAlarmData) {
        startPage();
        List<MrDeviceAlarmData> list = mrDeviceAlarmDataService.selectMrDeviceAlarmDataList(mrDeviceAlarmData);
        return getDataTable(list);
    }

    /**
     * 导出设备报警数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "设备报警数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MrDeviceAlarmData mrDeviceAlarmData) {
        List<MrDeviceAlarmData> list = mrDeviceAlarmDataService.selectMrDeviceAlarmDataList(mrDeviceAlarmData);
        ExcelUtil<MrDeviceAlarmData> util = new ExcelUtil<MrDeviceAlarmData>(MrDeviceAlarmData.class);
        util.exportExcel(response, list, "设备报警数据数据");
    }

    /**
     * 导出设备报警数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "设备报警数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export/currentAlarm")
    public void exportNow(HttpServletResponse response, MrDeviceAlarmData mrDeviceAlarmData) {
        List<MrDeviceAlarmData> list = mrDeviceAlarmDataService.currentAlarm(mrDeviceAlarmData);
        ExcelUtil<MrDeviceAlarmData> util = new ExcelUtil<MrDeviceAlarmData>(MrDeviceAlarmData.class);
        util.exportExcel(response, list, "设备报警数据数据");
    }

    /**
     * 打印设备报警数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:printData')")
    @Log(title = "设备报警数据", businessType = BusinessType.OTHER)
    @PostMapping("/printData")
    public void printData(HttpServletResponse response, @RequestBody MrDeviceAlarmData mrDeviceAlarmData) {
        List<MrDeviceAlarmData> list = mrDeviceAlarmDataService.selectMrDeviceAlarmDataList(mrDeviceAlarmData);
        List<MrDeviceAlarmDataVO> voList = new ArrayList<>();
        for (MrDeviceAlarmData deviceAlarm : list) {
            MrDeviceAlarmDataVO deviceAlarmDataVO = new MrDeviceAlarmDataVO();
//            deviceAlarmDataVO.setId(deviceAlarm.getId());
            deviceAlarmDataVO.setDeviceName(deviceAlarm.getDeviceName());
            deviceAlarmDataVO.setAlarmTime(deviceAlarm.getAlarmTime());
            deviceAlarmDataVO.setDeviceCode(deviceAlarm.getDeviceCode());
            deviceAlarmDataVO.setContent(deviceAlarm.getContent());
            // Set other properties as needed
            voList.add(deviceAlarmDataVO);
        }
        ExcelUtil<MrDeviceAlarmDataVO> util = new ExcelUtil<>(MrDeviceAlarmDataVO.class);

        String fileName = util.creatExcel(response, voList, "设备报警数据数据");
        String pdfName = "/home/psw/" + System.currentTimeMillis() + ".pdf";
        String printerName = sysDictDataService.selectDictValue("printer", "printer_name");
        PrintDemo.printPdf(fileName, pdfName, printerName);
        File file = new File(fileName);

        if (file.exists()) {
            if (file.delete()) {
                System.out.println("文件删除成功！");
            } else {
                System.out.println("文件删除失败！");
            }
        } else {
            System.out.println("文件不存在！");
        }
        File file2 = new File(pdfName);

        if (file2.exists()) {
            if (file2.delete()) {
                System.out.println("文件删除成功！");
            } else {
                System.out.println("文件删除失败！");
            }
        } else {
            System.out.println("文件不存在！");
        }

    }

    /**
     * 打印当前设备报警数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:printData')")
    @Log(title = "设备报警数据", businessType = BusinessType.OTHER)
    @PostMapping("/printData/currentAlarm")
    public void printDataNow(HttpServletResponse response, @RequestBody MrDeviceAlarmData mrDeviceAlarmData) {
        List<MrDeviceAlarmData> list = mrDeviceAlarmDataService.currentAlarm(mrDeviceAlarmData);
        List<MrDeviceAlarmDataVO> voList = new ArrayList<>();
        for (MrDeviceAlarmData deviceAlarm : list) {
            MrDeviceAlarmDataVO deviceAlarmDataVO = new MrDeviceAlarmDataVO();
//            deviceAlarmDataVO.setId(deviceAlarm.getId());
            deviceAlarmDataVO.setDeviceName(deviceAlarm.getDeviceName());
            deviceAlarmDataVO.setAlarmTime(deviceAlarm.getAlarmTime());
            deviceAlarmDataVO.setDeviceCode(deviceAlarm.getDeviceCode());
            deviceAlarmDataVO.setContent(deviceAlarm.getContent());
            // Set other properties as needed
            voList.add(deviceAlarmDataVO);
        }
        ExcelUtil<MrDeviceAlarmDataVO> util = new ExcelUtil<>(MrDeviceAlarmDataVO.class);

        String fileName = util.creatExcel(response, voList, "设备报警数据数据");
        String pdfName = "/home/psw/" + System.currentTimeMillis() + ".pdf";
        String printerName = sysDictDataService.selectDictValue("printer", "printer_name");
        PrintDemo.printPdf(fileName, pdfName, printerName);
        File file = new File(fileName);

        if (file.exists()) {
            if (file.delete()) {
                System.out.println("文件删除成功！");
            } else {
                System.out.println("文件删除失败！");
            }
        } else {
            System.out.println("文件不存在！");
        }
        File file2 = new File(pdfName);

        if (file2.exists()) {
            if (file2.delete()) {
                System.out.println("文件删除成功！");
            } else {
                System.out.println("文件删除失败！");
            }
        } else {
            System.out.println("文件不存在！");
        }

    }

    /**
     * 获取设备报警数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(mrDeviceAlarmDataService.selectMrDeviceAlarmDataById(id));
    }

    /**
     * 新增设备报警数据
     */
    @PreAuthorize("@ss.hasPermi('system:data:add')")
    @Log(title = "设备报警数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MrDeviceAlarmData mrDeviceAlarmData) {
        return toAjax(mrDeviceAlarmDataService.insertMrDeviceAlarmData(mrDeviceAlarmData));
    }

    /**
     * 修改设备报警数据
     */
    @PreAuthorize("@ss.hasPermi('system:data:edit')")
    @Log(title = "设备报警数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MrDeviceAlarmData mrDeviceAlarmData) {
        return toAjax(mrDeviceAlarmDataService.updateMrDeviceAlarmData(mrDeviceAlarmData));
    }

    /**
     * 删除设备报警数据
     */
    @PreAuthorize("@ss.hasPermi('system:data:remove')")
    @Log(title = "设备报警数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(mrDeviceAlarmDataService.deleteMrDeviceAlarmDataByIds(ids));
    }

    /**
     * 查询设备报警数据
     */
    @Log(title = "设备报警数据")
    @GetMapping("/listByUser")
    public R<Map<String, Object>> listByUser(MrDeviceAlarmData mrDeviceAlarmData) {

        // 2.查询设备报警数据
        List<MrDeviceAlarmData> list = mrDeviceAlarmDataService.getDeviceAlarmDataList(mrDeviceAlarmData);

        Map<String, Object> sysMsgMap = new HashMap<>();
        sysMsgMap.put("anntMsgList", list);
        sysMsgMap.put("anntMsgTotal", list.size());
        return R.ok(sysMsgMap);
    }

    @GetMapping("/queryCurrentAlerts")
    public R<Map<String, Object>> queryCurrentAlerts(MrDeviceAlarmData mrDeviceAlarmData) {

        // 2.查询设备报警数据
        List<CurrentAlertsVO> list = mrDeviceAlarmDataService.queryCurrentAlerts(mrDeviceAlarmData);

        Map<String, Object> sysMsgMap = new HashMap<>();
        sysMsgMap.put("anntMsgList", list);
        sysMsgMap.put("anntMsgTotal", list.size());
        return R.ok(sysMsgMap);
    }

    /**
     * 查询当前设备报警数据列表
     */
    @GetMapping("/currentAlarm")
    public TableDataInfo currentAlarm(MrDeviceAlarmData mrDeviceAlarmData) {
        startPage();
        List<MrDeviceAlarmData> list = mrDeviceAlarmDataService.currentAlarm(mrDeviceAlarmData);
        return getDataTable(list);
    }
}
